<?php
/**
 * Common Validation Utilities
 * 
 * @author Fallfish CIFIAS
 */

class Validator {
	public static function isEmail($email, $strict = false) {
		if ($strict) {
			$re = '/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i';
		}
		else
		{
			/* Cal Henderson: http://iamcal.com/publish/articles/php/parsing_email/pdf/
			* The long regular expression below is made by the following code
			* fragment:
			*
			*   $qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
			*   $dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
			*   $atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c'
			*         . '\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
			*   $quoted_pair = '\\x5c\\x00-\\x7f';
			*   $domain_literal = "\\x5b($dtext|$quoted_pair)*\\x5d";
			*   $quoted_string = "\\x22($qtext|$quoted_pair)*\\x22";
			*   $domain_ref = $atom;
			*   $sub_domain = "($domain_ref|$domain_literal)";
			*   $word = "($atom|$quoted_string)";
			*   $domain = "$sub_domain(\\x2e$sub_domain)*";
			*   $local_part = "$word(\\x2e$word)*";
			*   $addr_spec = "$local_part\\x40$domain";
			*/

			$re = '/^([^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-'
			.'\\x5d\\x7f-\\xff]+|\\x22([^\\x0d\\x22\\x5c\\x80-\\xff]|\\x5c\\x00-'
			.'\\x7f)*\\x22)(\\x2e([^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-'
			.'\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+|\\x22([^\\x0d\\x22\\x5c\\x80'
			.'-\\xff]|\\x5c\\x00-\\x7f)*\\x22))*\\x40([^\\x00-\\x20\\x22\\x28\\x29'
			.'\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+|\\x5b([^'
			.'\\x0d\\x5b-\\x5d\\x80-\\xff]|\\x5c\\x00-\\x7f)*\\x5d)(\\x2e([^\\x00-'
			.'\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-'
			.'\\xff]+|\\x5b([^\\x0d\\x5b-\\x5d\\x80-\\xff]|\\x5c\\x00-\\x7f)*'
			.'\\x5d))*$/'
			;
		}

		if (!preg_match($re, $email))
		{
			return false;
		}

		return true;
	}
	public static function isName($name)
	{
		if(ereg('^[a-zA-Z0-9_]{2,15}$',(string)$name))
		return true;
		else 
		return false;
	}
		public static function isPassword($pwd)
	{
		if(ereg('[/\'\"]',(string)$pwd))
		return false;
		else 
		return true;
	}
	public static function isPhoneNum($num) {
		if (ereg('^[0-9]{3,4}-([0-9]{5,10}(-[0-9]+)?)$',(string)$num))
		return true;
		else
		return false;
	}
	public static function isMobileNum($num) {
		if (ereg('^([+][0-9]{2,3})?[0-9]{9,15}$',(string)$num))
		return true;
		else
		return false;
	}
	public static function isZipCode($zip) {
		if (ereg('^[0-9]{6}$',(string)$zip))
		return true;
		else
		return false;
	}
	/**
	 * Check if the dateStr represents a date, expected format YYYY-MM-DD.
	 * 
	 * @author Fallfish CIFIAS
	 */
	public static function isDate($dateStr) {
		if (ereg('^[0-9]{2,4}-[0-9]{1,2}-[0-9]{1,2}$', $dateStr))
		return true;
		else
		return false;
	}
	public static function isValidDate($dateStr) {
		if (Validator::isDate($dateStr)) {
			$arr = explode('-', $dateStr);
			if(checkdate($arr[1], $arr[2], $arr[0]))
			return true;
			else
			return false;
		}
		else
		return false;
	}
	public static function isURL($url) {
		return eregi("(^|[ \t\r\n])((ftp|http|https|gopher|mailto|news|nntp|telnet|wais|file|prospero|aim|webcal):(([A-Za-z0-9\$_.+!*(), ;/?\:@&~=-])|%[A-Fa-f0-9]{2})+(#([a-zA-Z0-9][a-zA-Z0-9\$_.+!*(), ;/?:@&~=%-]*))?)", $url) ? true : false;
	}
	public static function isIP($ip)
	{
		if (ereg('^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$', $ip))
		return true;
		else
		return false;
	}

	/**
	 * Returns true if the input string consists of only whitespaces including \r\n
	 *
	 * @param string $s
	 * @return bool
	 */
	public static function isEmpty($s) {
		return strlen(preg_replace('/[\s\r\n]/', '', $s)) == 0;
	}

	/**
	 * Checks if a numeric value is within the given range
	 *
	 * @param numeric $val
	 * @param numeric $lowerBound
	 * @param numeric $upperBound
	 * @param string $boundSpecifier One of '()', '[]', '(]' and '[)'
	 * @return bool
	 */
	public static function inRange($val, $lowerBound, $upperBound, $boundSpecifier = '[]') {
		if (!in_array($boundSpecifier, array('()', '[]', '(]', '[)')))
		throw new Exception('Invalid boundSpecifier!');
		//list($l, $u) = explode('', $boundSpecifier);
		$l = $boundSpecifier[0];
		$u = $boundSpecifier[1];
		$closeLowerBound = $l == '[';
		$closeUpperBound = $u == ']';

		if (!is_int($val) && !is_float($val))
		if (!is_numeric($val))
		return false;
		else
		$val = floatval($val);
		return ($closeLowerBound ? $val >= $lowerBound : $val > $lowerBound) &&
		($closeUpperBound ? $val <= $upperBound : $val < $upperBound);
	}

	public static function isInt($val) {
		return (strval(intval($val)) == $val);
	}
	public static function isFloat($val) {
        //modified by xq262144
		return (!!preg_match('/^\d*\.\d+|\d+$/', $val));
	}
	
}